#!/bin/bash
##########################################################################
# Script_Name : Change-Gdm-Background-3.5.sh                             # 
# Description : Change background image of the GDM Login screen          #
# on ubuntu 18.04,20.04,22.10,22.04                                      #
# Date : Octobre 2022                                                    #
# written by : Griffon                                                   #
# Web Site :https://www.c-nergy.be - https://www.c-nergy.be/blog         #    
# Version : 3.5                                                          #
# History : 3.5 - Adding Support to Ubuntu 22.10                         #
#         : 3.5 - Remove Support End of Life Ubuntu                      #
#         : 3.4 - Adding Support to Ubuntu 22.04                         #
#         : 3.3 - Adding Support to Ubuntu 21.10                         #
#               - Removing Support to Ubuntu 20.10                       # 
#         : 3.2 - Adding Support to Ubuntu 21.04                         #
#               - Updating Code Picture background                       #
#           3.1 - Adding Support to Ubuntu 20.10                         #
#           3.0 - Adding Support to Ubuntu 20.04                         #
#               - New Code structure                                     #
#               - Added Solid Color Palette GUI                          #
#           2.0 - Updated Version to support 17.10,18.04,18.10           #   
#           1.0 - Initial Release Ubuntu 17.10                           # 
#                                                                        #
# Disclaimer : Script provided AS IS. Use it at your own risk....        #
#              You can use this script and distribute it as long as      #
#              credits are kept in place and unchanged                   #
#                                                                        # 
##########################################################################

#---------------------------------------------------#
# Script Version information Displayed              #
#---------------------------------------------------#

#---------------------------------------------------#
# Variables and Constants                           #
#---------------------------------------------------#

#--Automating Script versioning 
ScriptVer="3.5"
gdmpath="gdm3"

echo
/bin/echo -e "\e[1;36m   !----------------------------------------------------------------!\e[0m"
/bin/echo -e "\e[1;36m   !             Change-Gdm-Background script - Ver. $ScriptVer            !\e[0m"  
/bin/echo -e "\e[1;36m   !                Written by Griffon - Oct. 2022                  !\e[0m"
/bin/echo -e "\e[1;36m   !                      www.c-nergy.be                            !\e[0m"
/bin/echo -e "\e[1;36m   !----------------------------------------------------------------!\e[0m"
echo
/bin/echo -e "\e[1;38m   !----------------------------------------------------------------!\e[0m"
/bin/echo -e "\e[1;38m   !                      Disclaimer                                !\e[0m"
/bin/echo -e "\e[1;38m   !   !! Script provided AS IS. Use it at your own risk.!!         !\e[0m"
/bin/echo -e "\e[1;38m   !----------------------------------------------------------------!\e[0m"

echo
#--------------------------------------------------------------------------#
# -----------------------Function Section - DO NOT MODIFY -----------------#
#--------------------------------------------------------------------------#

#---------------------------------------------------#
# Function 0  - check for supported OS version  ....#
#---------------------------------------------------#

check_os()
{
echo
/bin/echo -e "\e[1;33m   |-| Detecting Ubuntu version        \e[0m"

case $version in

  *"Ubuntu 18.04"*)
   /bin/echo -e "\e[1;32m       |-| Ubuntu Version : $version\e[0m"
    call_gui_choice
	gdm_1804
    ;;

   *"Ubuntu 20.04"*)
   /bin/echo -e "\e[1;32m       |-| Ubuntu Version : $version\e[0m"
   call_gui_choice
   gdm_2004
    ;;
 
   *"Ubuntu 22.04"*)
   /bin/echo -e "\e[1;32m       |-| Ubuntu Version : $version\e[0m"
   gdmpath="gdm"
   call_gui_choice
   gdm_2004
    ;;

   *"Ubuntu 22.10"*)
   /bin/echo -e "\e[1;32m       |-| Ubuntu Version : $version\e[0m"
   #Ubuntu 21.10 - reference to gdm3 has been replaced with gdm
   gdmpath="gdm"
   call_gui_choice
   gdm_2004
    ;;


  *)
    /bin/echo -e "\e[1;31m  !--------------------------------------------------------------!\e[0m"
	/bin/echo -e "\e[1;31m  ! Your system is not running a supported version               !\e[0m"
	/bin/echo -e "\e[1;31m  ! The script has been tested only on the following versions    !\e[0m"
	/bin/echo -e "\e[1;31m  ! 18.04.x/20.04.x/22.04/22.10                                  !\e[0m"
	/bin/echo -e "\e[1;31m  ! The script is exiting...                                     !\e[0m"             
	/bin/echo -e "\e[1;31m  !--------------------------------------------------------------!\e[0m"
	echo
	exit
	;;

esac
echo
}

#--------------------------------------------------------------------#
# Function 1  - Display GUI for Change Login script                  #
#--------------------------------------------------------------------#

call_gui_choice(){

#------ Selection Dialog Box Displayed ----------------------------#

echo
/bin/echo -e "\e[1;33m   |-| Starting Wizard Interface        \e[0m"

cbxChoice=$(zenity --width="500" --height="190" --list \
--title="Login Screen Background Changer" \
--text "What do you want to do ?" \
--radiolist \
--column "Choice" \
--column "Description" \
FALSE "Solid color as background" \
FALSE "Picture as background" \
FALSE "Revert back to default" 2>/dev/null) 

#---------Check which option has been selected - Color/picture/Nothing------#

if [[ $cbxChoice = "Solid color as background" ]];
then

   /bin/echo -e "\e[1;32m       |-| Solid color selected..Proceeding\e[0m"
   selectcolor=$(zenity --color-selection --show-palette 2>/dev/null)
   if [ -z "$selectcolor" ]
   then
	/bin/echo -e "\e[1;31m       |-| No color has been chosen..Exiting\e[0m"       
 	exit 
   else 
      /bin/echo -e "\e[1;32m       |-| Solid color ........ : $selectcolor \e[0m"  	
   fi 
elif  [[ $cbxChoice = "Revert back to default" ]];
then
   /bin/echo -e "\e[1;31m       |-| Resetting Ubuntu default settings\e[0m"
   if  [[ "$version" = *"Ubuntu 18.04"* ]];
   then
	if [ -f /usr/share/gnome-shell/theme/ubuntu.css.griffon ]
	then
	/bin/echo -e "\e[1;32m       |-| Resetting Ubuntu 18.04 !\e[0m"
	sudo rm /usr/share/gnome-shell/theme/ubuntu.css
	sudo mv /usr/share/gnome-shell/theme/ubuntu.css.griffon /usr/share/gnome-shell/theme/ubuntu.css
	fi 
   else	
   	sudo update-alternatives --quiet --set $gdmpath-theme.gresource /usr/share/gnome-shell/theme/Yaru/gnome-shell-theme.gresource
   fi    
call_gui_rboot
   echo
elif  [[ $cbxChoice = "Picture as background" ]];
then
   /bin/echo -e "\e[1;32m       |-| Picture Selected...Proceeding\e[0m"  
   pix=$(zenity --file-selection --title="select the new login image" 2>/dev/null)
   if [ $? -eq 1 ]
   then
   	/bin/echo -e "\e[1;31m       |-| No Image Selected...Exiting\e[0m"  
        echo
	exit 
   else
	
	#--Get the File Name only not the full path -----## 
	fname=$(basename "$pix")
 	
   fi
else
	/bin/echo -e "\e[1;31m       |-| No option Selected...Exiting\e[0m" 
	echo
	exit 

fi

}

#--------------------------------------------------------------------#
# Function 2  - Reboot function                                      #
#--------------------------------------------------------------------#

call_gui_rboot(){

cbxReboot=$(zenity --width="500" --height="170" --list \
--title="Restart GDM Service or Reboot ?" \
--text "Select which option is best for you?" \
--radiolist \
--column "Choice" \
--column "Description" \
FALSE "Reboot Now (Recommended)" \
FALSE "Restart GDM service" 2>/dev/null) 


#---------Check which option has been selected - Color/picture/Nothing------#

if [[ $cbxReboot = "Reboot Now (Recommended)" ]];
then

	 /bin/echo -e "\e[1;32m       |-| want to reboot....;:-)   \e[0m"
	 sudo reboot
	 
elif [[ $cbxReboot  = "Restart GDM service" ]];
then
/bin/echo -e "\e[1;32m       |-| Restart GDM service....;:-)   \e[0m"
 if  [[ "$version" = *"Ubuntu 18.04"* ]];
   then
   zenity --info "Reboot Neeeded" --text "Performing GDM changes on Ubuntu 18.04 Require reboot. Please Manually Reboot your machine" --width 400
   else	 
	sudo service gdm restart
   fi
else
	/bin/echo -e "\e[1;31m       |-| Nothing here..... \e[0m" 
	echo
	exit 

fi
}


#--------------------------------------------------------------------#
# Function 3  - Update css file on Ubuntu 20.04 or later             #
#--------------------------------------------------------------------#

gdm_2004()
{

# Step 1 - Install prereqs for compilation later on
sudo apt-get -y install libglib2.0-dev-bin
sudo apt-get -y install libxml2-utils

#--Define some Variable
workdir="${HOME}/shell-theme"
xmlfile=${workdir}/custom.gresource.xml

echo
/bin/echo -e "\e[1;33m   |-| Creating Directory Structure        \e[0m"

if [ ! -d ${workdir} ]; then
	mkdir -p ${workdir}
fi

if [[ ! -d ${workdir}/Yaru ]]; then	
	mkdir -p ${workdir}/Yaru
fi

if [[ ! -d ${workdir}/Yaru-dark ]]; then	
	mkdir -p ${workdir}/Yaru-dark
fi

#Addition to support Ubuntu 21.04
if [[ ! -d ${workdir}/Yaru-light ]]; then	
	mkdir -p ${workdir}/Yaru-light
fi


if [[ ! -d ${workdir}/icons ]]; then	
	mkdir -p ${workdir}/icons
fi

if [[ ! -d ${workdir}/icons/scalable ]]; then	
	mkdir -p ${workdir}/icons/scalable
fi

if [[ ! -d ${workdir}/icons/scalable/actions ]]; then		
	mkdir -p ${workdir}/icons/scalable/actions
fi
if [[ ! -d ${workdir}/icons/scalable/status ]]; then		
	mkdir -p ${workdir}/icons/scalable/status
fi

echo
/bin/echo -e "\e[1;33m   |-| Creating xml file for compilation step       \e[0m"
bash -c "cat >"$xmlfile <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
 <gresource prefix="/org/gnome/shell/theme">
EOF

echo
/bin/echo -e "\e[1;32m       |-| Populate xml file...    \e[0m"   

#--Extract Resources -- Important location 
gst=/usr/share/gnome-shell/theme/Yaru/gnome-shell-theme.gresource
for r in `gresource list $gst`; do
	
       	gresource extract $gst $r >$workdir/${r#\/org\/gnome\/shell\/theme/} 
		if [ $? -eq 0 ]
		then
			echo "	<file>"${r#\/org\/gnome\/shell\/theme/}"</file>" >>$xmlfile
		fi
done

#Close the file 
echo "</gresource>" >>$xmlfile
echo "</gresources>" >>$xmlfile

echo
/bin/echo -e "\e[1;32m       |-| Modifying $gdmpath.css file...\e[0m"   

if [ -z "$selectcolor" ] 
then 
echo
	/bin/echo -e "\e[1;32m |-| Copying Picture to working Directory... \e[0m"
	cp "$pix" ${workdir}
	
	#Updated Code
	sed -i "/#lockDialogGroup {/!b;n;cbackground: #2c001e url('resource:///org/gnome/shell/theme/${fname}');\nbackground-repeat: no-repeat;\nbackground-size: cover;\nbackground-position: center;\n}" ${workdir}/$gdmpath.css  
	sed -i "/<file>$gdmpath.css<\/file>/a<file>${fname}<\/file>" $xmlfile
else
	#Find line #lockDialogGroup and replace line below with this code...
	sed -i "/#lockDialogGroup {/!b;n;cbackground-color: ${selectcolor}; }" ${workdir}/$gdmpath.css 
fi

/bin/echo -e "\e[1;32m       |-| Compile Resource File...\e[0m"   
cd ${workdir}
glib-compile-resources custom.gresource.xml
echo

/bin/echo -e "\e[1;33m |-| Create new Theme Folder... \e[0m"
if [ ! -d "/usr/share/gnome-shell/theme/custom" ]; then
	sudo mkdir /usr/share/gnome-shell/theme/custom
else
/bin/echo -e "\e[1;32m       |-| Folder exists...Proceeding  \e[0m"   

fi
/bin/echo -e "\e[1;32m       |-| Copy new gresource file...\e[0m"   
sudo cp ${workdir}/custom.gresource /usr/share/gnome-shell/theme/custom
/bin/echo -e "\e[1;32m       |-| Copy picture to new theme folder...\e[0m"   
#Not Needed actually
#sudo cp "$pix" /usr/share/gnome-shell/theme/custom
echo
/bin/echo -e "\e[1;33m |-| Applying Changes... \e[0m"
/bin/echo -e "\e[1;32m       |-| redirect to new theme folder...\e[0m"   
echo
sudo update-alternatives --quiet --install /usr/share/gnome-shell/$gdmpath-theme.gresource $gdmpath-theme.gresource /usr/share/gnome-shell/theme/custom/custom.gresource 0
sudo update-alternatives --quiet --set $gdmpath-theme.gresource /usr/share/gnome-shell/theme/custom/custom.gresource

echo
/bin/echo -e "\e[1;33m |-| Rebooting or Restarting GDM Service... \e[0m"

#Reboot Server or Restart GDM Service ...
call_gui_rboot


}

#--------------------------------------------------------------------#
# Function 4  - Update css file on Ubuntu 18.04                      #
#--------------------------------------------------------------------#

gdm_1804() 
{
echo
/bin/echo -e "\e[1;33m   |-| Check if script has been run already         \e[0m"

if [ -f /usr/share/gnome-shell/theme/ubuntu.css.griffon ]
then
	/bin/echo -e "\e[1;32m       |-| Script has already run...resetting !\e[0m"
	sudo rm /usr/share/gnome-shell/theme/ubuntu.css
	sudo mv /usr/share/gnome-shell/theme/ubuntu.css.griffon /usr/share/gnome-shell/theme/ubuntu.css
fi 
echo
/bin/echo -e "\e[1;33m   |-| Backup original ubuntu.css file...        \e[0m"

sudo cp /usr/share/gnome-shell/theme/ubuntu.css /usr/share/gnome-shell/theme/ubuntu.css.griffon

echo
/bin/echo -e "\e[1;33m   |-| Modifying css file used by GDM        \e[0m"

if [ -z "$selectcolor" ] 
then 
 	/bin/echo -e "\e[1;32m       |-| Update in progress ........  \e[0m"  
	sudo cp "$pix" /usr/share/backgrounds/
	sudo chmod og+r /usr/share/backgrounds/"${fname}"
	#-- Delete 2 lines after #lockDialogGroup
	sudo sed -i "/#lockDialogGroup {/{n;d}" /usr/share/gnome-shell/theme/ubuntu.css
	sudo sed -i "/#lockDialogGroup {/{n;d}" /usr/share/gnome-shell/theme/ubuntu.css
	#-- add modified Value
	sudo sed -i "/#lockDialogGroup {/a background: #2c001e url('file:///usr/share/backgrounds/${fname}');\nbackground-repeat: no-repeat;\nbackground-size: cover;\nbackground-position: center;\n}\n" /usr/share/gnome-shell/theme/ubuntu.css 

else
	/bin/echo -e "\e[1;32m       |-| Update in progress ........  \e[0m"  
	#-- Delete 2 lines after #lockDialogGroup
	sudo sed -i "/#lockDialogGroup {/{n;d}" /usr/share/gnome-shell/theme/ubuntu.css
	sudo sed -i "/#lockDialogGroup {/{n;d}" /usr/share/gnome-shell/theme/ubuntu.css
	#-- add modified Value
	sudo sed -i "/#lockDialogGroup {/a background: ${selectcolor}; }" /usr/share/gnome-shell/theme/ubuntu.css	
fi

echo
/bin/echo -e "\e[1;33m   |-| Changes Applied....        \e[0m"
#Reboot Server or Restart GDM Service ...
call_gui_rboot



}

#--------------------------------------------------------------------#
# Function 5  - Show Credits                                         #
#--------------------------------------------------------------------#

sh_credits() { 

/bin/echo -e "\e[1;36m   !-----------------------------------------------------!\e[0m"
/bin/echo -e "\e[1;36m   ! Change-Gdm-Background - Version : $ScriptVer               !\e[0m"
/bin/echo -e "\e[1;36m   ! Written By - Griffon - Oct. 2021                    !\e[0m"
/bin/echo -e "\e[1;36m   !                                                     !\e[0m"
/bin/echo -e "\e[1;36m   ! Web :www.c-nergy.be - www.c-nergy.be/blog           !\e[0m"
/bin/echo -e "\e[1;36m   !-----------------------------------------------------!\e[0m"
echo
}

#--------------------------------------------------------------------------#
# -----------------------END Function Section             -----------------#
#--------------------------------------------------------------------------#

#--------------------------------------------------------------------------#
#------------                 MAIN SCRIPT SECTION       -------------------#  
#--------------------------------------------------------------------------#

#---------------------------------------------------#
#-- Step 0 - Try to Detect Ubuntu Version.... 
#---------------------------------------------------#

version=$(lsb_release -sd) 
codename=$(lsb_release -sc) 

#Trick to get prompted for sudo password - Nothing will be logged
sudo touch gdmlogin.log
#Call Check OS Function to validate OS Version 
check_os
#End of script - Show Credits
sh_credits
